package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        log.info("查询>: {}", shoppingCartDTO);
        //先查询表中是否已经存在该购物信息 存在就更新number值 因为每次添加一次 这个add方法就会执行一次
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询
        ShoppingCart cartInfo = shoppingCartMapper.getCartInfo(shoppingCart);
        //判断
        if (cartInfo != null) {
            //已存在 更新number
            cartInfo.setNumber(cartInfo.getNumber() + 1);
            //重新更新
            shoppingCartMapper.updateNumber(cartInfo);
            return;
        }
        //不存在
        //判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            log.info("添加菜品");
            //是菜品
            //调用接口查找基本信息
            DishVO dish = dishMapper.getDishById(shoppingCart.getDishId());
            //拷贝属性
            // BeanUtils.copyProperties(dish,shoppingCart);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());

        } else {
            log.info("添加套餐");
            //是套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());

        }

        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //插入
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public List<ShoppingCart> searchShoppingCartInfo() {
    return shoppingCartMapper.searchShoppingCartInfo(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOneGoods(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteOneGoods(shoppingCart);
    }


}

